package com.ld.security.server.user.modules.rec.vo;

import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.anno.SqlKeys;
import com.ld.security.common.mybatis.base.vo.ProxyVo;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.server.user.modules.rec.entity.RecCardEntity;
import com.ld.security.server.user.modules.rec.entity.RecItemEntity;
import com.ld.security.server.user.modules.ups.constant.UpsKeywordConstant;
import com.ld.security.server.user.modules.ups.entity.UpsKeywordEntity;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

@Data
public class RunUserRecommendVo implements SuperVo,ProxyVo {

    /**
     * 物品开始时间
     */
    List<Date> timeScope;



    /**
     * 需要推荐的用户
     */
    List<String> upsUserIds;

    /**
     * 物品项
     */

    @SqlKey(name = "uptTime",value = SqlCondition.between,valueField = "timeScope")
    @QueryKey
    List<RecItemEntity> recItems;


    /**
     * 用户关键字
     */
    @QueryKey(groupKey = "upsUserId")
    @SqlKey(name = "upsUserId",value = SqlCondition.in,valueField = "upsUserIds")
    Map<String,List<UpsKeywordEntity>> mapList;


    /**
     * 物品打分
     */
    Map<String,Integer> itemMap;



    /**
     * 推荐集合
     */
    Map<String,List<RecCardEntity>> recMap;

    /**
     * 判断男用户是否包含关键字
     * @return
     */
    private UpsKeywordEntity containKeyword(UpsKeywordEntity upsKeywordEntity){

        List<UpsKeywordEntity> vals =  mapList.get(upsKeywordEntity.getUpsUserId());
        if(vals == null){
            return null;
        }
        for (UpsKeywordEntity val : vals) {
          if(
                  val.getWtType() == upsKeywordEntity.getWtType() &&
                  val.getKeyword().equals(upsKeywordEntity.getKeyword())){
              return val;
          }
        }
        return null;
    }

    private void incr(String upsUserId,String itemId,int val){
        String key = this.getKey(upsUserId,itemId);
        Integer v = itemMap.get(key);
        if(v == null){
            v= val;
        }else{
            v+=val;
        }
        itemMap.put( key, v);
    }

    private String getKey(String upsUserId,String itemId){
        String key = upsUserId+"-"+itemId;
        return key;
    }

    private void compItem(Integer size){

        for (String upsUserId : upsUserIds) {

            List<RecCardEntity> list =  new LinkedList<>();
            for (RecItemEntity recItem : recItems) {
                String key =  this.getKey(upsUserId,recItem.getId());
                Integer scope =  itemMap.get(key);
                if(scope == null){
                    scope = 0;
                }


                RecCardEntity card =      new RecCardEntity();
//                card.setLabel(recItem.getLabel());
//                card.setContentId(recItem.getContent());
//                card.setPicture(recItem.getPicture());

                card.setItemCode(recItem.getItemCode());
                card.setItemId(recItem.getId());
                card.setContentId(recItem.getItemId());
                card.setWt(scope);
                card.setUpsUserId(upsUserId);
                list.add(card);
            }

            list =  list.stream().sorted((x,y) -> {
                return y.getWt() - x.getWt();
            }).limit(size).collect(Collectors.toList());

            recMap.put(upsUserId,list);

        }
    }




    /**
     * 计算用户推荐的列表
     * @return
     */
    private void compUserRecItem(){
        for (String upsUserId : upsUserIds) {
            UpsKeywordEntity keywordEntity =     new UpsKeywordEntity();
            keywordEntity.setUpsUserId(upsUserId);
            for (RecItemEntity recItem : recItems) {
              for (Object o : recItem.getLabelKeys()) {
                  keywordEntity.setKeyword(o.toString());

                  keywordEntity.setWtType(UpsKeywordConstant.wtType.title);
                  UpsKeywordEntity key =  this.containKeyword(keywordEntity);
                  if(key != null){
                      this.incr(upsUserId,recItem.getId(),key.getWt());
                  }
              }

                for (Object o : recItem.getContentKeys()) {
                    keywordEntity.setKeyword(o.toString());

                    keywordEntity.setWtType(UpsKeywordConstant.wtType.content);
                    UpsKeywordEntity key =  this.containKeyword(keywordEntity);
                    if(key != null){
                        this.incr(upsUserId,recItem.getId(),key.getWt());
                    }
                }



                    keywordEntity.setKeyword(recItem.getContentType());
                    keywordEntity.setWtType(UpsKeywordConstant.wtType.content_type);
                    UpsKeywordEntity key =  this.containKeyword(keywordEntity);
                    if(key != null){
                        this.incr(upsUserId,recItem.getId(),key.getWt());
                    }


                keywordEntity.setKeyword(recItem.getContentType());
                keywordEntity.setWtType(UpsKeywordConstant.wtType.content_crt_user);
                key =  this.containKeyword(keywordEntity);
                if(key != null){
                    this.incr(upsUserId,recItem.getId(),key.getWt());
                }
          }
        }



    }

    @Override
    public void queryAfter() {
        itemMap = new HashMap<>();
        recMap = new HashMap<>();

        compUserRecItem();
        compItem(20);
    }
}
